﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;

namespace ERP.QuanlyKho
{
    public partial class viewERP_XuatTrungbay : DevExpress.XtraEditors.XtraForm
    {
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public int count = 0;
        public cs_erp_hanghoaInfo hanghoa = new cs_erp_hanghoaInfo();
        public cs_erp_hangtrungbayInfo tb = new cs_erp_hangtrungbayInfo();

        public viewERP_XuatTrungbay()
        {
            InitializeComponent();
        }

        private void viewERP_XuatTrungbay_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");

            ds = this.tb.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int k = 0;
            for (int i = 0; i < this.count; i++)
            {
                //if (Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) > 0)
                //{
                this.tb.id_chitiet = Convert.ToInt32(this.gridView1.GetRowCellValue(i,gridColumn13).ToString().Trim());
                
                this.tb.noidung = this.gridView1.GetRowCellValue(i, gridColumn11).ToString().Trim();
                try
                {
                    this.tb.sualoi = Convert.ToBoolean(this.gridView1.GetRowCellValue(i, gridColumn7).ToString().Trim());
                }
                catch
                {
                    this.tb.sualoi = false;
                }
                try
                {
                    this.tb.trungbay = Convert.ToBoolean(this.gridView1.GetRowCellValue(i, gridColumn6).ToString().Trim());
                }
                catch
                {
                    this.tb.trungbay = false;
                }
                try
                {
                    this.tb.soluong = Convert.ToDouble(this.gridView1.GetRowCellValue(i, gridColumn5).ToString().Trim());
                }
                catch
                {
                    this.tb.soluong = 0;
                }
                if (this.tb.sualoi == false && this.tb.trungbay == false)
                {
                    this.tb.ngaykt = DateTime.Now.ToShortDateString();
                }
                else
                {
                    this.tb.ngaythuchien = DateTime.Now;
                    this.tb.ngaykt = " ";
                }
                cs_erp_chitiet_nhapkhoInfo ct = new cs_erp_chitiet_nhapkhoInfo();
                ct.id_chitiet_nhapkho = this.tb.id_chitiet;
                ct.Get_Info();
                if (this.tb.soluong > ct.soluong)
                {
                    XtraMessageBox.Show("Bạn không thể trưng bày hoặc sửa lỗi số lượng hàng hóa > số lượng đã nhập!", "Thông báo");
                    return;
                }
                if(this.tb.soluong > 0)
                {
                    k += this.tb.ExcuteProcedure(1);
                }
            }
            if (k > 0)
            {
                XtraMessageBox.Show("Lưu danh sách hàng lỗi và hàng trưng bày thành công!", "Thông báo");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }
    }
}